package com.sunhao.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity12 extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_reslut;

    //第一个操作数
    private String firstNum = "";
    //运算符
    private String operator = "";

    //    第二个操作数
    private String secondNum = "";

    //当前的计算结果
    private String result = "";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main12);
        tv_reslut = findViewById(R.id.tv_result);
        View btn_1 = findViewById(R.id.btn_1);
        View btn_2 = findViewById(R.id.btn_2);
        View btn_3 = findViewById(R.id.btn_3);
        View btn_4 = findViewById(R.id.btn_4);
        View btn_5 = findViewById(R.id.btn_5);
        View btn_6 = findViewById(R.id.btn_6);
        View btn_7 = findViewById(R.id.btn_7);
        View btn_8 = findViewById(R.id.btn_8);
        View btn_9 = findViewById(R.id.btn_9);
        View btn_0 = findViewById(R.id.btn_0);
        View btn_CE = findViewById(R.id.btn_CE);
        View xie = findViewById(R.id.btn_xie);
        View btn_x = findViewById(R.id.btn_x);
        View btn_C = findViewById(R.id.btn_C);
        View btn_jia = findViewById(R.id.btn_jia);
        View btn_jian = findViewById(R.id.btn_jian);
        View btn_genhhao = findViewById(R.id.btn_genhao);
        View btn_dian = findViewById(R.id.btn_dian);
        View btn_dengyu = findViewById(R.id.btn_dengyu);
        View btn_fenzhi = findViewById(R.id.btn_fenzhi);
        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_CE.setOnClickListener(this);
        xie.setOnClickListener(this);
        btn_x.setOnClickListener(this);
        btn_C.setOnClickListener(this);
        btn_jia.setOnClickListener(this);
        btn_jian.setOnClickListener(this);
        btn_genhhao.setOnClickListener(this);
        btn_dian.setOnClickListener(this);
        btn_genhhao.setOnClickListener(this);
        btn_dengyu.setOnClickListener(this);
        btn_fenzhi.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_xie) {
            this.operator="/";
            firstNum+=firstNum;
        }
        if (v.getId() == R.id.btn_x) {
            this.operator="x";
            firstNum+=firstNum;
        }

        if (v.getId() == R.id.btn_CE) {
            tv_reslut.setText("0");
            firstNum="";
            operator="";
            secondNum="";
            result="";
        }
        if (v.getId() == R.id.btn_C) {
            //清除倒数第一位数
            String str = (String) tv_reslut.getText();
            if (str.length() == 1) {
                str = "0";
            }
            if (str.length() >1) {
                str = str.substring(0, str.length() - 1);
            }
            result=str;
            tv_reslut.setText(str);
        }

        if (v.getId() == R.id.btn_7) {
            firstNum += "7";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_8) {
            firstNum += "8";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_9) {
            firstNum += "9";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_4) {
            firstNum += "4";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_5) {
            firstNum += "5";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_6) {
            firstNum += "6";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_0) {
            firstNum += "0";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_1) {
            firstNum += "1";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_2) {
            firstNum += "2";
            tv_reslut.setText(firstNum);
        }
        if (v.getId() == R.id.btn_3) {
            firstNum += "3";
            tv_reslut.setText(firstNum);
        }
    }




}